import time
from . import ConnPG
import psycopg2
from sanic.log import logger
from .sku_account_table import SkuAccountTable
from setting import *


class MoveSkuLocation:

    HOST = HOST  # 在setting文件中设置
    dsn = f"host={PS_HOST} port={PS_PORT} user={PS_USER} password={PS_PASSWORD} dbname={PS_DATABASE}"

    def __init__(self):
        self.conn = psycopg2.connect(self.dsn)
        self.cur = self.conn.cursor(
            cursor_factory=psycopg2.extras.RealDictCursor)

    def move_sku_location(self,
                          from_location_id,
                          to_location_id,
                          sku_id,
                          value):

        # 参数检查
        if not all([from_location_id, to_location_id, sku_id, value]):

            return {
                'status': -1,
                'errorCode': 300100,
                'errorMsg': "接口有三个必传参数",
                'resultBody': None
            }

        sql = '''\
        INSERT INTO sku_account(
            in_or_out, sku_id, storage_location_id, quantity,
            create_time, status
            )
        VALUES (
            %s, %s, %s, %s, %s, %s)

        RETURNING id'''

        self.cur.execute(sql, (True, sku_id, to_location_id,
                               value, int(time.time()), 'success'))
        self.cur.execute(sql, (False, sku_id, from_location_id,
                               value, int(time.time()), 'success'))

        self.conn.commit()
        self.cur.close()
        self.conn.close()

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "移位成功",
        }

    # async def select_all_warehouse(self):#查看所有库房

    #     sql = 'select * from warehouse'

    #     result = await ConnPG().fetchall(sql)

    #     return {
    #         'status': 1,
    #         'errorCode': None,
    #         'errorMsg': None,
    #         'resultBody': result,
    #     }

    # async def select_all_warehouse_by_duty(self, duty_id):

    #     if duty_id == None or not duty_id.isdecimal():

    #         return {
    #             'status': -1,
    #             'errorCode': 300107,
    #             'errorMsg': '却少duty_id参数',
    #             'resultBody': None
    #         }

    #     sql_ = "select * from view_duty_warehouse where duty_id=%s"

    #     duty_result = await ConnPG().fetchall(sql_, (duty_id,))

    #     warehouse_list = [i['warehouse_id'] for i in duty_result]

    #     warehouse_list_str = ''

    #     for i in warehouse_list:

    #         warehouse_list_str = warehouse_list_str + f'or id={i} '

    #     sql = "select * from warehouse where " + warehouse_list_str[2:]

    #     result = await ConnPG().fetchall(sql)
    #     print(result)
    #     return {
    #         'status': 1,
    #         'errorCode': None,
    #         'errorMsg': None,
    #         'resultBody': result,
    #     }

    # async def del_warehouse(self, warehouse_id):#删除warehouse

    #     #参数检查

    #     if not warehouse_id or not warehouse_id.isdecimal:

    #         return {
    #             'status': -1,
    #             'errorCode': 300100,
    #             'errorMsg': '库房id不能为空且为整数类型',
    #             'resultBody': None
    #         }

    #     sql = 'DELETE FROM warehouse WHERE id=%s'

    #     try:
    #         await ConnPG().execute(sql, (warehouse_id,))
    #     except psycopg2.errors.ForeignKeyViolation as e:

    #         # print(e)

    #         return {

    #             'status': -1,
    #             'errorCode': 300112,
    #             'errorMsg': str(e),
    #             'resultBody': None,
    #         }
    #     else:

    #         return {
    #             'status': 1,
    #             'errorCode': None,
    #             'errorMsg': None,
    #             'resultBody': "公司或项目删除成功",
    #         }

    # async def update_warehouse(self, warehouse_id, warehouse_name):#更新库房名称

    #     #参数检查
    #     if not all([warehouse_id, warehouse_name]) or not warehouse_id.isdecimal():

    #         return {
    #             'status': -1,
    #             'errorCode': 300100,
    #             'errorMsg': '接口有两个必传参数且warehouse_id为整数',
    #             'resultBody': None
    #         }

    #     sql = "UPDATE warehouse SET warehouse_name=%s WHERE id=%s"

    #     await ConnPG().execute(sql, (warehouse_name,warehouse_id))

    #     return {
    #         'status': 1,
    #         'errorCode': None,
    #         'errorMsg': None,
    #         'resultBody': "名称修改成功",
    #     }
